<template>
	<scroll-view class="main" scroll-y>
		
		<view class="list">
			<view class="msg-card" @click="handleClick(item)" v-for="(item,index) in msgList" :key="index">				
				<view class="icon">
					<view class="badge" v-if="item.count>0">
						{{item.count}}
					</view>
					<image class="image" src="https://admin.cqycgyl.com/uploads/20250916/2fbec4daf4e522c90340eb09f00348ab.png" v-if="item.type==='kefu'"></image>
					<image class="image" src="https://admin.cqycgyl.com/uploads/20250916/7adb546376fde8464b4cd12837812552.png" v-if="item.type==='order'"></image>
					<image class="image" src="https://admin.cqycgyl.com/uploads/20250916/39646631cc6f0268206be567a69e334e.png" v-if="item.type==='service'"></image>
					<image class="image" src="https://admin.cqycgyl.com/uploads/20250916/c6c73d5a0cd8629883c4a8b22f9b6a10.png" v-if="item.type==='system'"></image>
				</view>
				<view class="info">
					<view class="title">{{item.title}}</view>
					<view class="msg">{{item.msg}}</view>
				</view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				msgList: [],
				
			}
		},
		
		onLoad(){
			
			// 先按照type分类，统计每种type的未读数量和最新一条消息
			
		},
		onShow(){
			this.list();
		},
		methods: {
			list(){
				let temp  =[
					{
						type: 'kefu',
						title: '在线客服',
						msg: '客服消息',
						time: new Date().getDate(),
						count: 0,
						url: '/pageA/subnotice'
					},
					{
						type: 'order',
						title: '订单消息',
						msg: '订单消息',
						time: new Date().getDate(),
						count: 0,
						url: '/pageA/subnotice'
					},
					{
						type: 'service',
						title: '服务通知',
						msg: '服务消息',
						time: new Date().getDate(),
						count: 0,
						url: '/pageA/subnotice'
					},
					{
						type: 'system',
						title: '系统消息',
						msg: '暂无消息',
						time: new Date().getDate(),
						count: 0,
						url: '/pageA/subnotice'
					},
					
				];

				this.$u.api.notice.getMessageList({
					"uid": this.vuex_user.user_id,
					"all": 0
				}).then(res => {
					
					let list = res.data;
					console.log('list',list)
					list.forEach(item => {
						if(item.is_read == 0){
							temp.forEach(item2 => {
								if(item2.type == JSON.parse(item.data).type){
									item2.count += 1;
									item2.msg = JSON.parse(item.data)?.msg||'暂无消息';
									item2.time = item.create_time_text;
								}
							});
						}
						
					});					
					
					this.msgList = temp;
					
					this.$u.vuex('vuex_msg', list.map(item=>JSON.parse(item.data)));
					let count = temp.reduce((acc, item) => acc + item.count, 0);	
					wx.setTabBarBadge({
						index: 2,
						text: ''+count
					});
				});
			
				
			},
			handleClick(item){
				if(item.url){
					uni.navigateTo({
						url: `/pageA/subnotice?type=${item.type}`
					})
				}else{
					
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main{
		width:100vw;
		height: 100vh;
		.list{
			height: 100%;
			width: 100%;
			display: flex;
			flex-direction: column;
			padding: 30rpx;			
			*{
				flex-shrink: 0;
			}
			.msg-card{
				width: 100%;
				padding-top: 30rpx;
				padding-bottom: 30rpx;
				display: flex;
				align-items: center;
				bottom: solid 1rpx #E5E5E5;
				.icon{
					width: 88rpx;
					height: 88rpx;
					position: relative;
					image{
						width: 100%;
						height: 100%;
					}
					.badge{
						width: 32rpx;
						height: 32rpx;
						background: #FF4D4F;
						border-radius: 50%;
						position: absolute;
						top: 0;
						right: -7rpx;
						font-size: 22rpx;
						color: #FFFFFF;
						text-align: center;
						line-height: 32rpx;
					}
				}

				.info{
					height: 88rpx;
					display: flex;
					flex-direction: column;
					margin-left: 20rpx;			
					justify-content: space-between;	
					.title{
						font-weight: bold;
						font-size: 32rpx;
						color: #333333;
					}

					.msg{

						font-weight: 400;
						font-size: 26rpx;
						color: #999999;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}

				.right{
					margin-left: auto;
					font-weight: 400;
					font-size: 22rpx;
					color: #999999;
					height: 100%;
				}
				
			}
		}
	}

</style>
